package thread;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;

import javax.swing.JPanel;

public class Ball extends Thread {
	JPanel panel;
	Point point;
	int size = 50;
	int half = size / 2;

	int mx = 10;
	int my = -10;

	int x, y; // 볼의 좌표
	Color color;
	public Ball(JPanel p, Point point) {
		this.panel = p;
		this.point = point;
		Random r = new Random();
		color = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
	}

	@Override
	public void run() {
		Graphics g = panel.getGraphics();
		g.setColor(Color.WHITE);
		g.drawOval(x, y, size, size);
		x = (int) point.getX() - half;
		y = (int) point.getY() - half;

		while (true) {
			g.setColor(Color.WHITE);
			g.drawOval(x, y, size, size);
			x += mx;
			y += my;

			if (x <= 10) {
				x = 10;
				mx = 10;
			}
			if (y <= 10) {
				y = 10;
				my = 10;
			}
			if (x >= panel.getWidth() - 60) {
				x = panel.getWidth() - 60;
				mx = -10;
			}
			if (y >= panel.getHeight() - 60) {
				y = panel.getHeight() - 60;
				my = -10;
			}
			g.setColor(color);
			g.drawOval(x, y, size, size);

			try {
				Thread.sleep(100);
			} catch (Exception ex) {
			}
		}
	}
}
